package copyengine.resource.resProxy.cacheData
{
    import com.catalystapps.gaf.core.ZipToGAFAssetConverter;
    import com.catalystapps.gaf.data.GAFBundle;

    import copyengine.log.CELog;
    import copyengine.utils.GeneralUtils;

    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;

    public class CEGAFResCacheData extends CEResCacheDataBasic
    {
        private var mURLLoader:URLLoader;
        private var mByteData:ByteArray;
        private var mGAFBundle:GAFBundle;

        public function CEGAFResCacheData() {
            super();
        }

        override protected function doLoadRes():void {
            mURLLoader = new URLLoader();
            mURLLoader.dataFormat = URLLoaderDataFormat.BINARY;
            addListener();
            mURLLoader.load(new URLRequest(mFileDesc.filePath));
        }

        override public function getData():* {return mGAFBundle;}


        override protected function dispose():void {
            if (mLoadStatus == LOAD_STATUS_LOADED) {
                mGAFBundle.dispose();
                mGAFBundle = null;
                mByteData.clear();
                mByteData = null;
                CELog.log("GAF of : " + mFileDesc.fileName + " has been dispose");
            } else {
                removeListener();
                CELog.err("Can not release GAF data as the file : " + mFileDesc.fileName + " not been loaded yet");
            }
            super.dispose();
        }

        //========================//
        //==Private  Function
        //=========================//

        private function addListener():void {
            GeneralUtils.addTargetEventListener(mURLLoader, Event.COMPLETE, onLoaded);
            GeneralUtils.addTargetEventListener(mURLLoader, ProgressEvent.PROGRESS, onLoadInProgress);
            GeneralUtils.addTargetEventListener(mURLLoader, IOErrorEvent.IO_ERROR, onLoadInError);
        }

        private function removeListener():void {
            GeneralUtils.removeTargetEventListener(mURLLoader, Event.COMPLETE, onLoaded);
            GeneralUtils.removeTargetEventListener(mURLLoader, ProgressEvent.PROGRESS, onLoadInProgress);
            GeneralUtils.removeTargetEventListener(mURLLoader, IOErrorEvent.IO_ERROR, onLoadInError);
        }

        private function onLoaded(e:Event):void {
            mByteData = mURLLoader.data as ByteArray;

            var converter:ZipToGAFAssetConverter = new ZipToGAFAssetConverter();
            converter.addEventListener(Event.COMPLETE, onZipConvertedComplete);
            converter.convert(mByteData);
        }

        private function onZipConvertedComplete(e:Event):void {
            mGAFBundle = (e.target as ZipToGAFAssetConverter).gafBundle;

            removeListener();
            onResLoadComplete();
            //Loader资源可以释放掉
            mURLLoader = null;
        }

        private function onLoadInProgress(e:ProgressEvent):void {
        }

        private function onLoadInError(e:IOErrorEvent):void {
            removeListener();
            CELog.err(e.toString());
            CELog.err("CECompressedObjResCacheData-> load file in error, file: " + mFileDesc.toString());
        }
    }
}
